﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class DONTHUOC_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.DONTHUOC_Get_List();


            foreach (var t in types)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_DON_THUOC";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            DONTHUOC_Get_ListResult temp = new DONTHUOC_Get_ListResult();

            temp.MA_DON_THUOC = "...";
            temp.TEN_TOA = "Thêm mới";
            cmb.Items.Add(temp);
        }
        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.DONTHUOC_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "DT0001";
            }
            else
            {
                for (int i = 1; i <= (rowcount + 1); i++)

                {
                    string IDtam = "DT";
                    for (int j = 0; j < (4 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.DONTHUOC_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_DON_THUOC.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }

        public void addData(string ma_dt, ComboBox cmbBacSi, ComboBox cmbNhomBenh, TextBox txtTen_Toa, TextBox txtDien_Giai)
        {
            if (validateData(cmbBacSi, cmbNhomBenh, txtTen_Toa) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {

                db.DONTHUOC_Insert(ma_dt, cmbBacSi.SelectedValue.ToString(), cmbNhomBenh.SelectedValue.ToString(), txtTen_Toa.Text, txtDien_Giai.Text);
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(ComboBox cmbBacSi, ComboBox cmbNhomVT, TextBox txtTen_Toa)
        {
            if (cmbBacSi.SelectedItem == null || cmbNhomVT.SelectedItem==null || txtTen_Toa.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();

            var list = db.DONTHUOC_Get_List();

            foreach (var t in list)
            {
                List.Items.Add(t);
            }
        }
        public void loadData(DataPager dp)
        {

            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();

            var dtbs = db.DONTHUOC_Get_List().Join(db.BACSI_Get_List(),p=>p.MA_BACSI,c=>c.MA_BACSI,(p,c)=>new{p.MA_BACSI,p.MA_DON_THUOC,p.MA_NHOMBENH,p.TEN_TOA,p.DIEN_GIAI,c.HO_TEN});
            var dtbsnb = dtbs.Join(db.NHOMBENH_Get_List(), p => p.MA_NHOMBENH, c => c.MA_NHOMBENH, (p, c) => new { p.TEN_TOA,p.MA_NHOMBENH,p.MA_DON_THUOC,p.MA_BACSI,p.HO_TEN,p.DIEN_GIAI,c.TEN_NHOMBENH });
            var typeorder = dtbsnb.OrderBy(w => w.TEN_TOA);
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.TEN_TOA.Trim() != "")
                {
                    dpSource.Add(new { STT = i, u.DIEN_GIAI, u.HO_TEN, u.MA_BACSI, u.MA_DON_THUOC, u.MA_NHOMBENH, u.TEN_NHOMBENH, u.TEN_TOA });
                    i++;

                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string ma_dt, ComboBox cmbBacSi, ComboBox cmbNhomBenh, TextBox txtTen_Toa, RichTextBox txtDien_Giai)
        {
            if (validateData(cmbBacSi, cmbNhomBenh, txtTen_Toa) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                
                db.DONTHUOC_Update(ma_dt, ma_dt, cmbBacSi.SelectedValue.ToString(), cmbNhomBenh.SelectedValue.ToString(), txtTen_Toa.Text, txtDien_Giai.Document.Blocks.ToString());
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {

            error = "";
            if (db.DONTHUOC_CT_GetByMaDonThuoc(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.DONTHUOC_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;

        }
        BACSI_Control ctrl1 = new BACSI_Control();
        NHOMBENH_Control ctrl2 = new NHOMBENH_Control();
        public void loadForm_Edit(string ID_Edit,  ComboBox cmbBacSi, ComboBox cmbNhomBenh, TextBox txtTen_Toa, RichTextBox txtDien_Giai)
        {

            var sl = (DONTHUOC_GetByMaDonThuocResult)db.DONTHUOC_GetByMaDonThuoc(ID_Edit).Single();


            

            ctrl1.loadComboBox(cmbBacSi);

            cmbBacSi.SelectedValue = sl.MA_BACSI;

            ctrl2.loadComboBox(cmbNhomBenh);

            cmbNhomBenh.SelectedValue = sl.MA_NHOMBENH;

            txtTen_Toa.Text = sl.TEN_TOA;

            txtDien_Giai.AppendText( sl.DIEN_GIAI);
        }
    }
}
